<template>
    <el-input v-model="amountInWords" v-bind="$attrs" v-on="$listeners" disabled>
    </el-input>
</template>
<script>
    export default {
        name:"AmountInWords",
        props:["value","amount"],
        computed:{
            amountInWords:{
                get(){
                    return this.value
                },
                set(v){
                    this.$emit("input",v)
                }
            }
        },
        watch:{
            amount(v){
                this.amountInWords = this.digitUppercase(v)
            }
        },
        methods:{
            // 金额数字转大写
            digitUppercase(price) {
                const fraction = ['角', '分']
                const digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
                const unit = [
                    ['元', '万', '亿'],
                    ['', '拾', '佰', '仟']
                ]
                let num = Math.abs(price)
                let s = ''
                fraction.forEach((item, index) => {
                    s += (digit[Math.floor(num * 10 * (10 ** index)) % 10] + item).replace(/零./, '')
                })
                s = s || '整'
                num = Math.floor(num)
                for (let i = 0; i < unit[0].length && num > 0; i += 1) {
                    let p = ''
                    for (let j = 0; j < unit[1].length && num > 0; j += 1) {
                        p = digit[num % 10] + unit[1][j] + p
                        num = Math.floor(num / 10)
                    }
                    s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s
                }
                return s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整')
            }
        }
    }
</script>
